<?php
     include_once('classes/class.mysql.php');
    
     session_start();
    
     $db = new MySQL();
     $comtec_id_user = $_GET["id"];
     $type = $_GET['t'];
     $result = $db->fetch_array($db->query("SELECT comtec_id_mainteiner FROM users WHERE comtec_id_user = ". $comtec_id_user));   
     $comtec_id_mainteiner = $result['comtec_id_mainteiner'];
     
     $tsql = "SELECT o.cod_order,o.comtec_id_order,o.operation_type,m.cod_mainteiner,m.brand,m.model, CONCAT_WS(' ',r.name,r.lastname1,r.lastname2) AS encargado, ow.company_name,DATE_FORMAT(o.order_start_date,'%d/%m/%Y') as fecha, 
              DATE_FORMAT((SELECT op.operation_date_end FROM operation as op WHERE op.comtec_id_machine = m.comtec_id_machine AND op.status_reg=1 ORDER BY op.operation_date_end DESC LIMIT 1),'%d/%m/%Y') AS last_operation, 
              (SELECT COUNT(*) FROM orders as o1 INNER JOIN operation as op1 ON o1.comtec_id_order = op1.comtec_id_order WHERE o1.comtec_id_order = o.comtec_id_order AND o1.status_reg=1 AND op1.status_reg=1 ) as cant FROM ((orders as o INNER JOIN machines as m ON o.comtec_id_machine = m.comtec_id_machine) INNER JOIN owner as ow ON m.comtec_id_owner = ow.comtec_id_owner) INNER JOIN responsible as r ON r.comtec_id_responsible = o.comtec_id_responsible 
              WHERE o.comtec_id_mainteiner = $comtec_id_mainteiner AND o.status_reg=1";
     
     if($type == 1){
         $tsql .=" AND o.order_start_date >= curdate() ORDER BY o.order_start_date ASC  ";
     }else{
        $tsql .=" AND o.order_start_date < curdate() ORDER BY o.order_start_date ASC ";  
     }
    
    $stmt = $db->query($tsql);
     
    $texto = "<table class='table'>
               <tr>";   
    $texto .= "<th colspan=3></th>";
    $texto .= "<th>C&oacute;digo</th>
              <th>Tipo</th>
              <th>C&oacute;d. M&aacute;quina</th>
              <th>Marca</th>
              <th>Modelo</th>            
              <th>Propietario</th>
              <th>Encargado</th>
              <th>Fecha últ. oper.</th>
              <th>Fecha a operar</th>
              ";
    $texto .= "</tr>";  
    $cont = 0;
     while ($row = mysql_fetch_array($stmt, MYSQL_ASSOC)){ 
      $cant=$row['cant'];
      if($cant == 0){
       $cont += 1;
       $cod_order=$row['cod_order'];
       $comtec_id_order=$row['comtec_id_order'];
       $operation_type=$row['operation_type'];
       $cod_mainteiner=$row['cod_mainteiner'];
       $brand=$row['brand'];
       $model=$row['model'];
       $encargado=$row['encargado'];
       $company_name=$row['company_name'];
       $fecha=$row['fecha'];
       $last_operation=$row['last_operation'];
       $texto  .= "<tr>";
       $url_edit = 'http://'.$_SERVER['HTTP_HOST'].'/iconos/edit.png';
       $texto  .= "<td><a href='javascript:loadOrder(".$comtec_id_order.");'><img src='$url_edit' alt='Editar' title='Editar orden de trabajo' style='border: 0px;' /></a></td>";
       $url_delete = 'http://'.$_SERVER['HTTP_HOST'].'/iconos/delete.png';
       $texto  .= "<td><a href='javascript:deleteOrder(".$comtec_id_order.");'><img src='$url_delete' alt='Eliminar' title='Eliminar orden de trabajo' style='border: 0px;' /></a></td>";      
       $url_add = 'http://'.$_SERVER['HTTP_HOST'].'/iconos/addOperation.png';
       $texto  .= "<td><a href='javascript:loadAddOperation(".$comtec_id_order.");'><img src='$url_add' alt='Ingresar operacion' title='Ingresar operacion' style='border: 0px;' /></a></td>";     
       $cod_order   = ($cod_order != "")? $cod_order : "-";
       $texto .= "<td><a href='javascript:order(".$comtec_id_order.");' title='Ver orden de trabajo'>$cod_order</a></td>";
       $Tipo = "-";
       switch ($operation_type) {
        case 1: 
             $Tipo = "Mantenci&oacute;n";
        break;
        case 2: 
             $Tipo = "Emergencia";
        break; 
        case 3: 
             $Tipo = "Instalaci&oacute;n";
        break;
        case 4:
             $Tipo = "Reparaci&oacute;n";
        break;    
       }
       $texto  .= "<td>$Tipo</td>";     
       $cod_mainteiner   = ($cod_mainteiner != "")? $cod_mainteiner : "-";
       $texto  .= "<td>$cod_mainteiner</td>";
       $brand   = ($brand != "")? $brand : "-";
       $texto  .= "<td>$brand</td>";
       $model   = ($model != "")? $model : "-";
       $texto  .= "<td>$model</td>";
       $texto  .= "<td>$company_name</td>";
       $texto  .= "<td>$encargado</td>";
       $last_operation   = ($last_operation != "")? $last_operation : "-";
       $texto  .= "<td>$last_operation</td>";
       $texto  .= "<td>$fecha</td>"; 
       $texto .= "</tr>";
      }
     }
    $texto .= "</table>";
    echo "<p class='total'>Total: $cont</p>".$texto."<div id='result'></div>";    
?>
